﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Menu : System.Web.UI.Page
{
    RestaurantBooking.DishesDataTable dishesDataTable = null;
    RestaurantBookingTableAdapters.DishesTableAdapter dishesAdapter = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        dishesDataTable = new RestaurantBooking.DishesDataTable();
        dishesAdapter = new RestaurantBookingTableAdapters.DishesTableAdapter();

        dishesAdapter.Fill(dishesDataTable);
    }

    protected void DishesListView_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        DishesListView.EditIndex = e.NewEditIndex;
    }

    protected void lbnAddtoMenu_OnClick(object sender, CommandEventArgs e)
    {
        RestaurantBooking.DishesRow dish = dishesDataTable.FindByID(Int32.Parse(e.CommandArgument.ToString()));
        RestaurantBooking.DishesDataTable menu = (RestaurantBooking.DishesDataTable)Session["Menu"];

        bool f = false;
        foreach (RestaurantBooking.DishesRow d in menu)
        {
            if (d.Name == dish.Name)
            {
                if (d.RowState == DataRowState.Deleted)
                {
                    d.RejectChanges();
                }
                f = true;
                break;
            }
        }

        if (f == false)
        {
            menu.AddDishesRow(dish.Name,dish.Image,dish.Price);
        }
        ((MasterPage)Master).MenuDataBind();
    }
    protected void DishesListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (Session["Authentication"] == null)
            {
                e.Item.FindControl("divAdminOnly").Visible = false;
            }
            else if(!Session["Authentication"].Equals("Administrator"))
            {
                e.Item.FindControl("divAdminOnly").Visible = false;
            }
        }
    }
    protected void DishesListView_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        DishesListView.EditIndex = -1;
    }
        
    protected void DishesListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        try
        {
            Label lblID = (Label)DishesListView.Items[e.ItemIndex].FindControl("lblID");

            RestaurantBooking.DishesRow dish = dishesDataTable.FindByID(Int32.Parse(lblID.Text));
            if (dish != null)
            {
                dish.Name = e.NewValues["Name"].ToString();
                dish.Image = e.NewValues["Image"].ToString();
                dish.Price = Int32.Parse(e.NewValues["Price"].ToString());
                dishesAdapter.Update(dish);

                RestaurantBooking.DishesDataTable menu = (RestaurantBooking.DishesDataTable)Session["Menu"];
                foreach (RestaurantBooking.DishesRow d in menu)
                {
                    if (d.Name.Equals(e.OldValues["Name"].ToString()))
                    {
                        d.Name = dish.Name;
                        d.Image = dish.Image;
                        d.Price = dish.Price;
                        ((MasterPage)Master).MenuDataBind();
                        break;
                    }
                    
                }
                
            }
        }
        catch (Exception ex)
        {
            e.Cancel = true;
            DishesListView.EditIndex = -1;
        }
    }
    protected void DishesListView_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        Label lblID = (Label) DishesListView.Items[e.ItemIndex].FindControl("lblID");

        RestaurantBooking.DishesRow dish = dishesDataTable.FindByID(Int32.Parse(lblID.Text));
        if (dish != null)
        {
            RestaurantBooking.DishesDataTable menu = (RestaurantBooking.DishesDataTable)Session["Menu"];
            foreach (RestaurantBooking.DishesRow d in menu)
            {
                if (d.Name.Equals(dish.Name))
                {
                    d.Delete();
                    ((MasterPage)Master).MenuDataBind();
                    break;
                }

            }
            dish.Delete();
            dishesAdapter.Update(dish);
        }
    }
}
